package cn.andy.android.crm.fragment;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Service;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.os.Bundle;
import android.os.Vibrator;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import cn.andy.android.crm.R;

public class ShakeDemoFragment extends Fragment implements SensorEventListener{
	
	private static final int SPEED_SHRESHOLD = 2000;
	
	private static final int UPDATE_INTERVAL_TIME = 70;
	
	private long lastupdatetime;
	private float lastX;
	private float lastY;
	private float lastZ;
	
	private SensorManager sensorManager;
	private Vibrator vibrator;

	SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.fragment_shakedemo, container, false);
		
		sensorManager = (SensorManager) getActivity().getSystemService(Service.SENSOR_SERVICE);
		
		vibrator = (Vibrator) getActivity().getSystemService(Service.VIBRATOR_SERVICE);
		
		
		
		return view;
	}
	
	@Override
	public void onResume() {
		super.onResume();
		
		sensorManager.registerListener(this, sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER), SensorManager.SENSOR_DELAY_NORMAL);
		
	}
	
	@Override
	public void onStop() {
		sensorManager.unregisterListener(this);
		super.onStop();
	}
	
	@Override
	public void onPause() {
		sensorManager.unregisterListener(this);
		super.onPause();
	}

	public void onAccuracyChanged(Sensor sensor, int accuracy) {
		
	}

	public void onSensorChanged(SensorEvent event) {
		long currupdatetime = System.currentTimeMillis();
		long timeinterval = currupdatetime - lastupdatetime;
		if(timeinterval < UPDATE_INTERVAL_TIME)
			return;
		
		lastupdatetime = currupdatetime;
		
		float x = event.values[0];
		float y = event.values[1];
		float z = event.values[2];
		
		float deltaX = x - lastX;
		float deltaY = y - lastY;
		float deltaZ = z - lastZ;
		
		double speed = Math.sqrt(deltaX*deltaX+deltaY*deltaY+deltaZ*deltaZ)/timeinterval*10000;
		
		boolean speedenough = speed > SPEED_SHRESHOLD;
		
		
		int sensortype = event.sensor.getType();
		
		float[] values = event.values;
		
		if(sensortype == Sensor.TYPE_ACCELEROMETER){
			int fx = Math.abs(values[0]) > 12 ? 1 : 0;
			int fy = Math.abs(values[1]) > 12 ? 1 : 0;
			int fz = Math.abs(values[2]) > 12 ? 1 : 0;
			if(Math.sqrt(values[0]*values[0]+values[1]*values[1]+values[2]*values[2]) > 20 &&speedenough){
				Toast.makeText(getActivity(), "I am shaking, ops.. at "+ sdf.format(new Date()), Toast.LENGTH_SHORT).show();
				vibrator.vibrate(500l);
			}
		}
	}

}
